/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-02 11:18:21;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */

$init(function() {
    //查询数据
    var id = getQueryString("id");
    $("#id").val(id);
    var query = getQueryString("query");
    request(base_gateway + query, { id: id }, function(ret) {
        var code = ret.code;
        if (code == 200 && !isEmpty(ret.data)) {

            document.getElementById('form-list').innerHTML = baidu.template('tpl-list', ret);
            getTableList(ret.data.table_name);
            getDictList(ret.data.dict_code);

            form.render('select', null);
        }
    });
    form.on('select(data_src_type)', function(data) {
        var fieldSrc = $("textarea[name='custom_src']").val();
        var str = "";
        if (fieldSrc == "") {
            if (data.value == 'db-table') {
                var json = {
                    "api": "",
                    "table": "",
                    "key": "id",
                    "value": "id,xxx",
                    "search": "xxx",
                    "cut": "-"
                }
                str = JSON.stringify(json);
            } else if (data.value == 'tree-parent') {
                var json = {
                    "api": "sys/catalog/lists",
                    "pid": "0",
                    "table": "sys_catalog",
                    "key": "id",
                    "value": "name",
                    "cut": "-"
                }
                str = JSON.stringify(json);
            }
            $("textarea[name='custom_src']").val(str);
            formatJson(str, -1);
        }
    });
    form.on('select(input_type)', function(data) {
        var inputType = $("select[name='input_type']").val();
        var fieldSrc = $("textarea[name='custom_src']").val();
        var str = "";
        if (fieldSrc == "") {
            if (data.value == 'singleFile' || data.value == 'multiFile' || data.value == 'singleFileRemarks' || data.value == 'multiFileRemarks') {
                var json = {
                    "exts": "",
                    "path": "",
                    "thumb": ""
                }
                str = JSON.stringify(json);
                $("textarea[name='custom_src']").val(str);
                formatJson(str, -1);
            }
            $("textarea[name='custom_src']").val(str);
            formatJson(str, -1);
        }
        if (inputType == 'textEditor' || inputType == 'singleFile' || inputType == 'multiFile' || inputType == 'singleFileRemarks' || inputType == 'multiFileRemarks') {
            $("select[name='is_allow_replace']").val(1);
        } else {
            $("select[name='is_allow_replace']").val(-1);
        }
        form.render('select', null);
    });

    form.on('select(field_type)', function(data) {
        if (data.value == 'int') {
            $("input[name='field_len']").val(10);
        } else if (data.value == 'bigint') {
            $("input[name='field_len']").val(19);
        } else if (data.value == 'tinyint') {
            $("input[name='field_len']").val(3);
        } else if (data.value == 'decimal') {
            $("input[name='field_len']").val('10,2');
        } else if (data.value == 'char') {
            $("input[name='field_len']").val(20);
        } else if (data.value == 'varchar') {
            $("input[name='field_len']").val(50);
        } else {
            $("input[name='field_len']").val(0);
        }
        if (data.value == 'int' || data.value == 'bigint' || data.value == 'tinyint') {
            if ($("input[name='field_default_value']").val() == '') {
                $("input[name='field_default_value']").val(0);
            }
        } else if (data.value == 'decimal') {
            if ($("input[name='field_default_value']").val() == '') {
                $("input[name='field_default_value']").val('0.00');
            }
        } else {
            if ($("input[name='field_default_value']").val() == 0 || $("input[name='field_default_value']").val() == 0.00) {
                $("input[name='field_default_value']").val('');
            }
        }
        if (data.value == "datetime") {
            $("input[name='field_default_value']").val('1970-01-01 08:00:00');
        }
        if (data.value == "date") {
            $("input[name='field_default_value']").val('1970-01-01');
        }
        if (data.value == "time") {
            $("input[name='field_default_value']").val('00:00:00');
        }

    });
    var gateway = getQueryString("gateway");
    frameSubmit(base_gateway + gateway, true, function(ret) {

    });
});

function getDictList(_dictCode) {
    var data = {
        "page": 1,
        "limit": 10000
    };
    request(base_gateway + 'sys/dict/lists', data, function(ret) {
        var code = ret.code;
        if (code == 200 && ret.data.rows.length > 0) {
            var str = '<option value="">数据字典</option>';
            for (var i = 0; i < ret.data.rows.length; i++) {
                var selected = "";
                if (_dictCode == ret.data.rows[i].dict_code) {
                    selected = "selected"
                }
                str += '<option value="' + ret.data.rows[i].dict_code + '" ' + selected + '>' + ret.data.rows[i].dict_name + " / " + ret.data.rows[i].dict_code + '</option>';
            }
            $("select[name='dict_code']").html(str);
            form.render('select', null);
        }

    });
}

function getTableList(_tableName) {
    var data = {
        "page": 1,
        "limit": 10000
    };
    request(base_gateway + 'sys/table/lists', data, function(ret) {
        var code = ret.code;
        if (code == 200 && ret.data.rows.length > 0) {
            var str = '<option value="">数据表</option>';
            for (var i = 0; i < ret.data.rows.length; i++) {
                var selected = "";
                if (_tableName == ret.data.rows[i].table_name) {
                    selected = "selected"
                }
                str += '<option value="' + ret.data.rows[i].table_name + '" ' + selected + '>' + ret.data.rows[i].table_remarks + " / " + ret.data.rows[i].table_name + '</option>';
            }
            $("select[name='table_name']").html(str);
            $("select[name='table_name']").attr("disabled", true);
            form.render('select', null);
        }

    });
}

function formatJson(json, tag) {
    try {
        if (typeof JSON.parse(json) == "object") {
            if (tag == 1) {
                layer.msg('校验通过', {
                    shade: [0.1, '#393D49'],
                    time: 2000,
                    icon: 6
                });
            }
            $("textarea[name='custom_src']").val(JSON.stringify(JSON.parse(json), null, 4));
            return true;
        }
    } catch (e) {}
    if (tag == 1) {
        layer.msg('JSON格式不正确', {
            shade: [0.1, '#393D49'],
            time: 2000,
            icon: 5
        });
    }
    return false;
}